package com.ruoyi.project.mahjong.service.impl;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.mahjong.domain.dto.CostThreeChildDto;
import com.ruoyi.project.mahjong.domain.dto.CostThreeDto;
import com.ruoyi.project.mahjong.domain.vo.CostThreeVo;
import com.ruoyi.project.remake.domain.constant.InitConstant;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.mahjong.mapper.TCostMapper;
import com.ruoyi.project.mahjong.domain.TCost;
import com.ruoyi.project.mahjong.service.ITCostService;

/**
 * 计费Service业务层处理
 *
 * @author ruoyi
 * @date 2023-10-09
 */
@Service
public class TCostServiceImpl implements ITCostService
{
    @Autowired
    private TCostMapper tCostMapper;

    /**
     * 查询计费
     *
     * @param id 计费ID
     * @return 计费
     */
    @Override
    public TCost selectTCostById(Long id)
    {
        return tCostMapper.selectTCostById(id);
    }

    /**
     * 查询计费列表
     *
     * @param tCost 计费
     * @return 计费
     */
    @Override
    public List<TCost> selectTCostList(TCost tCost)
    {
        return tCostMapper.selectTCostList(tCost);
    }

    /**
     * 新增计费
     *
     * @param tCost 计费
     * @return 结果
     */
    @Override
    public int insertTCost(TCost tCost)
    {
        tCost.setCreateTime(DateUtils.getNowDate());
        if (tCost.getCostId()==0L){
            tCost.setCostName("按照分钟收费");
        } else if (tCost.getCostId()==1L) {
            tCost.setCostName("按照包场收费");
        } else if (tCost.getCostId()==2L) {
            tCost.setCostName("按分时段包段收费");
        }
        return tCostMapper.insertTCost(tCost);
    }

    /**
     * 修改计费
     *
     * @param tCost 计费
     * @return 结果
     */
    @Override
    public int updateTCost(TCost tCost)
    {
        tCost.setUpdateTime(DateUtils.getNowDate());
        return tCostMapper.updateTCost(tCost);
    }

    /**
     * 批量删除计费
     *
     * @param ids 需要删除的计费ID
     * @return 结果
     */
    @Override
    public int deleteTCostByIds(Long[] ids)
    {
        return tCostMapper.deleteTCostByIds(ids);
    }

    /**
     * 删除计费信息
     *
     * @param id 计费ID
     * @return 结果
     */
    @Override
    public int deleteTCostById(Long id)
    {
        return tCostMapper.deleteTCostById(id);
    }

    @Override
    public void saveThreeConfig(CostThreeDto costThreeDto) {
        saveOrUpdate(costThreeDto.getOneDto(),costThreeDto);
        saveOrUpdate(costThreeDto.getTwoDto(),costThreeDto);
        saveOrUpdate(costThreeDto.getThreeDto(),costThreeDto);
        saveOrUpdate(costThreeDto.getFoureDto(),costThreeDto);

    }

    @Override
    public CostThreeVo threeConfig(Long shopId,String roomId) {
        CostThreeVo vo=new CostThreeVo();
        TCost cost=new TCost();
        cost.setBak2(shopId.toString());
        cost.setBak1(roomId);
        cost.setCostId(InitConstant.THREE_COST_ID);
        List<TCost> costList=tCostMapper.selectTCostList(cost);
        CostThreeChildDto oneDto=new CostThreeChildDto();
        CostThreeChildDto twoDto=new CostThreeChildDto();
        CostThreeChildDto threeDto=new CostThreeChildDto();
        CostThreeChildDto fourDto=new CostThreeChildDto();
        if(ObjectUtil.isNotNull(costList)){
            if(costList.size()>=1){
                TCost oneVo=costList.get(0);
                BeanUtils.copyProperties(oneVo,oneDto);
            }
            if(costList.size()>=2){
                TCost twoVo=costList.get(1);
                BeanUtils.copyProperties(twoVo,twoDto);
            }
            if(costList.size()>=3){
                TCost threeVo=costList.get(2);
                BeanUtils.copyProperties(threeVo,threeDto);
            }
            if(costList.size()>=4){
                TCost foureVo=costList.get(3);
                BeanUtils.copyProperties(foureVo,fourDto);
            }
        }
        vo.setOneDto(oneDto);
        vo.setTwoDto(twoDto);
        vo.setThreeDto(threeDto);
        vo.setFoureDto(fourDto);
        return vo;
    }

    private void saveOrUpdate(CostThreeChildDto one,CostThreeDto costThreeDto) {
        TCost cost=new TCost();
        BeanUtils.copyProperties(one,cost);
        cost.setBak2(costThreeDto.getShopId().toString());
        cost.setCostId(costThreeDto.getCostId());
        cost.setBak1(costThreeDto.getRoomId());
        if(one.getId()==null){
            insertTCost(cost);
        }else{
            updateTCost(cost);
        }
    }
}
